﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using CafeOnline.Common.Criteria;
using CafeOnline.Domain.Entities;
using CafeOnline.Domain.IRepositories;
using LinqKit;

namespace CafeOnline.Domain.Repository {
    [Export(typeof(IProductGroupRepository))]
    [PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.Shared)]
    public class ProductGroupRepository : Repository<ProductGroup,ProductGroupCriteria>, IProductGroupRepository {

        public IEnumerable<ProductGroup> GetData(ProductGroupCriteria c) {
            return Find(c);
        }
        protected override Expression<Func<ProductGroup, bool>> BuildFilterPredicate(ProductGroupCriteria c) {
            var predicate = PredicateBuilder.True<ProductGroup>();
            if (!string.IsNullOrEmpty(c.CategoryName)) {
                predicate = predicate.And(x => x.ProductGroupName.Contains(c.CategoryName));
            }
            if (!string.IsNullOrEmpty(c.Description)) {
                predicate = predicate.And(x => x.ProductGroupDesc.Contains(c.Description));
            }
            if (c.IsActive) {
                predicate = predicate.And(x=> x.IsActive);
            }
            
            return predicate.Expand();
        }


        public int CountData(ProductGroupCriteria c) {
            return Count(c);
        }


        public IEnumerable<ProductGroup> GetAll() {
            return All();
        }

        public void AddNewProductGroup(ProductGroup entity) {
            Create(entity);
        }
    }
}
